package com.tz.bookstore.listener;

import org.apache.log4j.Logger;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

/**
 * Created by yejf on 2017/1/11.
 */
@WebListener
public class ContextPathListener implements ServletContextListener {

    private static Logger LOGGER = Logger.getLogger(ContextPathListener.class);

    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        LOGGER.debug("-- 应用上下文初始化....");
        ServletContext context = servletContextEvent.getServletContext();
        //
        String path = context.getContextPath();
        //
        LOGGER.debug("--把应用上下文绑定到application范围: "+path);
        context.setAttribute("path", path);
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
        LOGGER.debug("-- 应用销毁...");
    }
}
